import 'package:flutter/material.dart';
import 'main.dart';

class HomePage extends StatefulWidget {
  // const HomePage({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;

  void _onItemTap(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  TabController _tabController; // 定义一个controller

  // 使用自定义的Tab
  final List<Tab> tabs = <Tab>[
    Tab(
      // text: '新闻',
      // icon: Icon(Icons.next_week),
      child: new MyTabItem(0, '新闻'),
    ),
    Tab(
      // text: '历史',
      // icon: Icon(Icons.history),
      child: new MyTabItem(1, '历史'),
    ),
    Tab(
      // text: '图片',
      // icon: Icon(Icons.picture_in_picture),
      child: new MyTabItem(2, '图片'),
    ),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //创建_tabController
    _tabController = TabController(length: tabs.length, vsync: this)
      ..addListener(() {
        print('${_tabController.index}');
        switch (_tabController.index) {
          case 0:
            break;
          case 1:
            break;
          case 2:
            break;
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      // key: ,
      appBar: new AppBar(
        // 导航栏
        title: TabBar(
          // tabbar菜单
          controller: _tabController,
          tabs: tabs,
          indicatorColor: Colors.white,
          unselectedLabelColor: Colors.white60,
          labelStyle: TextStyle(fontSize: 16),
          // unselectedLabelStyle: TextStyle(fontSize: 14),
          indicatorWeight: 1, // 下划线高度
          isScrollable: false, // 是否可以滑动
          onTap: (index) {
            print(index);
          },
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {},
          ),
        ],
        leading: Builder(builder: (context) {
          return new IconButton(
            icon: Icon(Icons.dashboard),
            onPressed: () {
              //Scaffold.of(context).openDrawer();
              // 或者使用globalkey打开
              // _globalKey.currentState.openDrawer();
              openDrawer();
            },
          );
        }),
        bottom: TabBar(
          // tabbar菜单
          controller: _tabController,
          tabs: tabs,
          indicatorColor: Colors.white,
          unselectedLabelColor: Colors.white60,
          labelStyle: TextStyle(fontSize: 16),
          // unselectedLabelStyle: TextStyle(fontSize: 14),
          indicatorWeight: 1, // 下划线高度
          isScrollable: false, // 是否可以滑动
          onTap: (index) {
            print(index);
          },
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Center(
            child: new Text('新闻', style: TextStyle(fontSize: 20)),
          ),
          new Center(
            child: new Text('历史', style: TextStyle(fontSize: 20)),
          ),
          new Center(
            child: new Text('图片', style: TextStyle(fontSize: 20)),
          ),
        ],
      ),
    );
  }
}

class MyTabItem extends StatelessWidget {
  const MyTabItem(this.index, this.title) : super();
  final int index;
  final String title;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    // 使用流式布局wrap或Row都行
    // isScrollable: true, // 是否可以滑动 注意为true，否则宽度不能自适应

    StatelessWidget icon;
    switch (index) {
      case 0:
        icon = Icon(Icons.info);
        break;
      case 1:
        icon = Icon(Icons.history);
        break;
      default:
        icon = Icon(Icons.picture_in_picture);
        break;
    }
    return new Wrap(
      spacing: 4,
      children: <Widget>[
        icon,
        new Text(title),
      ],
    );
  }
}
